//
//  CZInteractionViewController.swift
//  GZWeibo666
//
//  Created by vision on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

enum InteractionType:String
{
    case comment = "comment"
    case forward = "forward"
}


class CZInteractionViewController: UIViewController {
    
    var status:CZStatus?
    var account:String?
    
    var type:InteractionType = InteractionType.comment
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        prepareTableView()
    }
    
    func prepareTableView()
    {

         let name = account
        var title:String?

        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        if self.type == InteractionType.comment
        {
             navigationItem.rightBarButtonItem = UIBarButtonItem(title: "评论", style: UIBarButtonItemStyle.Plain, target: self, action: "comment")
            title = "评论微博\n"
            
        }
       else
        {
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "转发", style: UIBarButtonItemStyle.Plain, target: self, action: "forward")
            
             title = "转发微博\n"
        }
        view.addSubview(content)
        content.frame = view.bounds
        
        let titleName = title! + name!
       
        // 创建可变的属性文本
        let attrString = NSMutableAttributedString(string: titleName)
        
        // 创建label
        let label = UILabel()
        
        // 设置属性文本
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        
        // 获取NSRange
        let nameRange = (titleName as NSString).rangeOfString(name!)
        
        // 设置属性文本的属性
        attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
        attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
        
       
        label.attributedText = attrString
        label.sizeToFit()
        
        navigationItem.titleView = label

    }
    
    /// 关闭控制器
    func close() {
//        // 关闭SV
//        SVProgressHUD.dismiss()
        
        self.view.endEditing(true)
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    ///评论
    func comment()
    {
         SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
        print("pinglun")
        
        if !content.text.isEmpty
        {
        
        if status != nil
        {
            CZNetworkTool.sharedInstance.advertiseComment(content.text!, id: (status?.id)!, finished: { (result, error) -> () in
                SVProgressHUD.dismiss()
                if error != nil && result == nil
                {
                    print("Ping轮失败\(error)")
                 SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                }
                
                else
                {
                print("评论成功!")
                     self.close()
                }
               
            })
        }
            else
        {
            SVProgressHUD.showErrorWithStatus("网络异常")
            }
        }
        else
        {
            SVProgressHUD.showErrorWithStatus("内容不能为空")
        }
    }
    
    ///转发
    func forward()
    {
         SVProgressHUD.showWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
        SVProgressHUD.showWithStatus("正在转发")
        if status != nil
        {
            CZNetworkTool.sharedInstance.forwardStatus(content.text ?? "转发微博", id: (status?.id)!, finished: { (result, error) -> () in
                if error != nil && result == nil
                {
                    print("转发\(error)")
              SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                   
                }
                    
                else
                {
                    print("转发成功!")
                    self.close()
                }
                
            })
        }
    }


    ///懒加载text
    private lazy var content:UITextView = {
    
        let textField = UITextView()
        textField.backgroundColor = UIColor.grayColor()
        textField.textAlignment = NSTextAlignment.Left
//        textField
        
        return textField
    }()
    
    
}


